home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 123 / cdrom123.iso / essenc / extens / wweb / Web Developer.xpi / chrome / webdeveloper.jar / content / webdeveloper / tools.js < prev    next >
Encoding:
JavaScript  |  2004-11-21  |  11.6 KB  |  291 lines

  1. // Displays the tools menu
  2. function webdeveloper_displayToolsMenu(menu, separatorName, tooltips)
  3. {
  4.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  5.     const validatorSeparator = menu.getElementsByAttribute("id", separatorName)[0];
  6.  
  7.     var description           = null;
  8.     var descriptionPreference = null;
  9.     var key                   = null;
  10.     var menuItem              = document.createElement("menuitem");
  11.     var url                   = null;
  12.     var toolsCount            = 0;
  13.  
  14.     // If the tools count preference is set
  15.     if(preferencesService.prefHasUserValue("webdeveloper.tool.count"))
  16.     {
  17.         toolsCount = preferencesService.getIntPref("webdeveloper.tool.count");
  18.     }
  19.  
  20.     webdeveloper_removeGeneratedMenuItems(menu);
  21.  
  22.     // Loop through the possible tools
  23.     for(var i = 1; i <= toolsCount; i++)
  24.     {
  25.         description = "webdeveloper.tool." + i + ".description";
  26.         key         = "webdeveloper.tool." + i + ".key";
  27.         url         = "webdeveloper.tool." + i + ".url";
  28.  
  29.         // If the description and URL are set
  30.         if(preferencesService.prefHasUserValue(description) && preferencesService.prefHasUserValue(url))
  31.         {
  32.             descriptionPreference = preferencesService.getCharPref(description).trim();
  33.  
  34.             // If the description is not blank
  35.             if(descriptionPreference != "")
  36.             {
  37.                 menuItem = document.createElement("menuitem");
  38.                 menuItem.setAttribute("class", "webdeveloper-generated-menu");
  39.                 menuItem.setAttribute("label", descriptionPreference);
  40.                 menuItem.setAttribute("oncommand", "webdeveloper_loadURL('" + preferencesService.getCharPref(url).trim() + escape(window.content.document.documentURI) + "')");
  41.  
  42.                 // If the key preference is set
  43.                 if(preferencesService.prefHasUserValue(key))
  44.                 {
  45.                     menuItem.setAttribute("key", key);
  46.                 }
  47.  
  48.                 // If displaying tooltips
  49.                 if(tooltips)
  50.                 {
  51.                     menuItem.setAttribute("tooltiptext", descriptionPreference);
  52.                 }
  53.  
  54.                 menu.insertBefore(menuItem, validatorSeparator);
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60. // Validates local CSS
  61. function webdeveloper_validateLocalCSS()
  62. {
  63.     const content            = window.document.getElementById("content");
  64.     const mainTabBox         = content.mTabBox;
  65.     const documentList       = webdeveloper_getDocuments(content.browsers[mainTabBox.selectedIndex].contentWindow, new Array());
  66.     const oldTab             = getBrowser().selectedTab;
  67.     const oldURL             = window.content.document.documentURI;
  68.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  69.     const request            = new XMLHttpRequest();
  70.  
  71.     var cssRule        = null;
  72.     var formElement    = null;
  73.     var generatedPage  = null;
  74.     var inputElement   = null;
  75.     var pageDocument   = null;
  76.     var styleSheet     = null;
  77.     var styleSheetHref = null;
  78.     var styleSheetList = null;
  79.     var styleText      = "";
  80.  
  81.     // Loop through the documents
  82.     for(var i = 0; i < documentList.length; i++)
  83.     {
  84.         pageDocument   = documentList[i];
  85.         styleSheetList = pageDocument.styleSheets;
  86.  
  87.         // Loop through the style sheets
  88.         for(var j = 0; j < styleSheetList.length; j++)
  89.         {
  90.             styleText += webdeveloper_retrieveStyleSheetText(pageDocument, styleSheetList[j]);
  91.         }
  92.     }
  93.  
  94.     generatedPage = webdeveloper_generatePage("");
  95.  
  96.     // This must be done to make generated content render
  97.     request.open("GET", "about:blank", false);
  98.     request.send("");
  99.  
  100.     formElement = generatedPage.content.document.createElement("form")
  101.     formElement.setAttribute("method", "get");
  102.     formElement.setAttribute("action", "http://jigsaw.w3.org/css-validator/validator");
  103.  
  104.     inputElement = generatedPage.content.document.createElement("input");
  105.     inputElement.setAttribute("type", "hidden");
  106.     inputElement.setAttribute("name", "profile");
  107.     inputElement.setAttribute("value", "css2");
  108.     formElement.appendChild(inputElement);
  109.  
  110.     inputElement = generatedPage.content.document.createElement("input");
  111.     inputElement.setAttribute("type", "hidden");
  112.     inputElement.setAttribute("name", "usermedium");
  113.     inputElement.setAttribute("value", "all");
  114.     formElement.appendChild(inputElement);
  115.  
  116.     inputElement = generatedPage.content.document.createElement("input");
  117.     inputElement.setAttribute("type", "hidden");
  118.     inputElement.setAttribute("name", "warning");
  119.     inputElement.setAttribute("value", "2");
  120.     formElement.appendChild(inputElement);
  121.  
  122.     inputElement = generatedPage.content.document.createElement("input")
  123.     inputElement.setAttribute("type", "hidden");
  124.     inputElement.setAttribute("name", "text");
  125.     inputElement.setAttribute("value", styleText);
  126.     formElement.appendChild(inputElement);
  127.  
  128.     generatedPage.content.document.body.appendChild(formElement);
  129.     formElement.submit();
  130.  
  131.     // If the open tabs in background preference is set to true
  132.     if(preferencesService.prefHasUserValue("webdeveloper.open.tabs.background") && preferencesService.getBoolPref("webdeveloper.open.tabs.background"))
  133.     {
  134.         getBrowser().selectedTab = oldTab;
  135.     }
  136. }
  137.  
  138. // Validates a local HTML file
  139. function webdeveloper_validateLocalHTML()
  140. {
  141.     const oldTab             = getBrowser().selectedTab;
  142.     const oldURL             = window.content.document.documentURI;
  143.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  144.     const request            = new XMLHttpRequest();
  145.  
  146.     var formElement   = null;
  147.     var generatedPage = null;
  148.     var inputElement  = null;
  149.  
  150.     generatedPage = webdeveloper_generatePage("");
  151.  
  152.     // This must be done to make generated content render
  153.     request.open("GET", oldURL, false);
  154.     request.send("");
  155.  
  156.     formElement = generatedPage.content.document.createElement("form")
  157.     formElement.setAttribute("method", "post");
  158.     formElement.setAttribute("action", "http://validator.w3.org/check");
  159.  
  160.     inputElement = generatedPage.content.document.createElement("input");
  161.     inputElement.setAttribute("type", "hidden");
  162.     inputElement.setAttribute("name", "verbose");
  163.     inputElement.setAttribute("value", "1");
  164.     formElement.appendChild(inputElement);
  165.  
  166.     inputElement = generatedPage.content.document.createElement("input");
  167.     inputElement.setAttribute("type", "hidden");
  168.     inputElement.setAttribute("name", "fragment");
  169.     inputElement.setAttribute("value", request.responseText);
  170.     formElement.appendChild(inputElement);
  171.  
  172.     generatedPage.content.document.body.appendChild(formElement);
  173.     formElement.submit();
  174.  
  175.     // If the open tabs in background preference is set to true
  176.     if(preferencesService.prefHasUserValue("webdeveloper.open.tabs.background") && preferencesService.getBoolPref("webdeveloper.open.tabs.background"))
  177.     {
  178.         getBrowser().selectedTab = oldTab;
  179.     }
  180. }
  181.  
  182. // Tidies the HTML before validating
  183. function webdeveloper_validateTidiedHTML()
  184. {
  185.     const oldTab             = getBrowser().selectedTab;
  186.     const oldURL             = window.content.document.documentURI;
  187.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  188.     const request            = new XMLHttpRequest();
  189.  
  190.     var formElement   = null;
  191.     var generatedPage = null;
  192.     var inputElement  = null;
  193.     var output        = "";
  194.  
  195.     generatedPage = webdeveloper_generatePage("");
  196.  
  197.     // This must be done to make generated content render
  198.     request.open("GET", oldURL, false);
  199.     request.send("");
  200.  
  201.     output = request.responseText;
  202.  
  203.     // If closing <br> tags
  204.     if(preferencesService.prefHasUserValue("webdeveloper.tidied.html.close.br") && preferencesService.getBoolPref("webdeveloper.tidied.html.close.br"))
  205.     {
  206.         output = output.replace(new RegExp("<br>", "gi"), "<br />");
  207.     }
  208.  
  209.     // If escaping ampersands
  210.     if(preferencesService.prefHasUserValue("webdeveloper.tidied.html.escape.ampersand") && preferencesService.getBoolPref("webdeveloper.tidied.html.escape.ampersand"))
  211.     {
  212.         output = output.replace(new RegExp("&(?!#?[xX]?(?:[0-9a-fA-F]+|\w{1,8});)", "gi"), "&");
  213.     }
  214.  
  215.     formElement = generatedPage.content.document.createElement("form")
  216.     formElement.setAttribute("method", "post");
  217.     formElement.setAttribute("action", "http://validator.w3.org/check");
  218.  
  219.     inputElement = generatedPage.content.document.createElement("input");
  220.     inputElement.setAttribute("type", "hidden");
  221.     inputElement.setAttribute("name", "verbose");
  222.     inputElement.setAttribute("value", "1");
  223.     formElement.appendChild(inputElement);
  224.  
  225.     inputElement = generatedPage.content.document.createElement("input");
  226.     inputElement.setAttribute("type", "hidden");
  227.     inputElement.setAttribute("name", "doctype");
  228.     inputElement.setAttribute("value", "HTML 4.01 Transitional");
  229.     formElement.appendChild(inputElement);
  230.  
  231.     inputElement = generatedPage.content.document.createElement("input");
  232.     inputElement.setAttribute("type", "hidden");
  233.     inputElement.setAttribute("name", "fbd");
  234.     inputElement.setAttribute("value", "1");
  235.     formElement.appendChild(inputElement);
  236.  
  237.     inputElement = generatedPage.content.document.createElement("input");
  238.     inputElement.setAttribute("type", "hidden");
  239.     inputElement.setAttribute("name", "ss");
  240.     inputElement.setAttribute("value", "1");
  241.     formElement.appendChild(inputElement);
  242.  
  243.     inputElement = generatedPage.content.document.createElement("input");
  244.     inputElement.setAttribute("type", "hidden");
  245.     inputElement.setAttribute("name", "fragment");
  246.     inputElement.setAttribute("value", output);
  247.     formElement.appendChild(inputElement);
  248.  
  249.     generatedPage.content.document.body.appendChild(formElement);
  250.     formElement.submit();
  251.  
  252.     // If the open tabs in background preference is set to true
  253.     if(preferencesService.prefHasUserValue("webdeveloper.open.tabs.background") && preferencesService.getBoolPref("webdeveloper.open.tabs.background"))
  254.     {
  255.         getBrowser().selectedTab = oldTab;
  256.     }
  257. }
  258.  
  259. // Validates the page against the validator
  260. function webdeveloper_validateURL(validator)
  261. {
  262.     const preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
  263.  
  264.     var validatorURL = "";
  265.  
  266.     // If the validator preference is set
  267.     if(preferencesService.prefHasUserValue(validator) && preferencesService.getCharPref(validator).trim() != "")
  268.     {
  269.         validatorURL = preferencesService.getCharPref(validator).trim();
  270.     }
  271.     else
  272.     {
  273.         const stringBundle = document.getElementById("webdeveloper-string-bundle");
  274.  
  275.         alert(stringBundle.getString("webdeveloper_validateURLConfigure"));
  276.         webdeveloper_options();
  277.  
  278.         // If the validator preference is set
  279.         if(preferencesService.prefHasUserValue(validator))
  280.         {
  281.             validatorURL = preferencesService.getCharPref(validator).trim();
  282.         }
  283.     }
  284.  
  285.     // If the validator URL is not empty
  286.     if(validatorURL != "")
  287.     {
  288.         webdeveloper_loadURL(validatorURL + escape(window.content.document.location.href));
  289.     }
  290. }
  291.